package com.mangowang.module.main.ui;

/**
 * 文件名 : EarningDetailActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:06
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.help.UserHelper;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Mystock;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.MachinesAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MachinesActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.tv_title_right)
    TextView tvTitleRight;

    @BindView(R2.id.tv_wei)
    TextView tv_wei;
    @BindView(R2.id.tv_bind)
    TextView tv_bind;
    @BindView(R2.id.tv_total)
    TextView tv_total;

    private MachinesAdapter mAdapter;
    private List<Mystock.myMachineGcTypeList.goodsInfo> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machines);
        unbinder = ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#FFFFFF"), true);
        tvTitleRight.setText("库存记录");
        tvTitle.setText("我的库存");
        tvTitleRight.setOnClickListener(v -> {
            startActivity(ChuRuRecordActivity.class);
        });
        backIv.setOnClickListener(v -> finish());
        initRecyclerView();
        initData();

    }


    private void initData() {
        Mystock.myMachine.Builder builder = Mystock.myMachine.newBuilder();
        builder.setOthersMemberId(UserHelper.getUser().getMember_id());
        NetworkManager.INSTANCE.post(Apis.myMachineGcTypeList, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Mystock.myMachineGcTypeList result = Mystock.myMachineGcTypeList.parseFrom(response);
                Log.e("wang", "onOk: " + result.toString());
                List<Mystock.myMachineGcTypeList.goodsInfo> list = result.getGoodsInfoListList();
                tv_total.setText(result.getUnbindNum());
                tv_bind.setText(result.getActivationNum());
                tv_wei.setText(result.getUnactivationNum());
                mAdapter.addData(list);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });


    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new MachinesAdapter(mContext, datas));
        mAdapter.addChildClickViewIds(R.id.ll_action, R.id.ll_un_action,R.id.ll_on_bine);
        // 每个item的点击事件
        // 设置子控件点击监听
        mAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            Mystock.myMachineGcTypeList.goodsInfo goodsInfo =  mAdapter.getItem(position);
            String  titles =  goodsInfo.getGoodsName()+" " + goodsInfo.getGoodsSerial();
            if (view.getId() == R.id.ll_un_action) {
                Intent intent = new Intent(mContext, MachinesNextActivity.class);
                intent.putExtra("title", "未激活 " +titles + " "
                        + goodsInfo.getGoodsSerial());
                intent.setType(goodsInfo.getGoodsCommonid());
                intent.setAction("3"); // 未激活
                startActivity(intent);
            } else if (view.getId() == R.id.ll_action) {
                Intent intent1 = new Intent(mContext, MachinesNextActivity.class);
                intent1.putExtra("title", "激活 " + titles + " "
                        + goodsInfo.getGoodsSerial());
                intent1.setType(goodsInfo.getGoodsCommonid());
                intent1.setAction("2"); // 激活
                startActivity(intent1);
            } else if (view.getId() == R.id.ll_on_bine) {
                Intent intent1 = new Intent(mContext, MachinesNextActivity.class);
                intent1.putExtra("title", "未绑定 " + titles + " "
                        + goodsInfo.getGoodsSerial());
                intent1.setType(goodsInfo.getGoodsCommonid());
                intent1.setAction("1"); // 未绑定
                startActivity(intent1);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
    }


}
